/*
    Project:        Home Power Control
    File:           HPCWindVane.h
    Author:         christian@karunamusic.at
    Date:           11.2014
    Description:    Sensor to measure the direction of the wind.
                    The sensor works with 8 reed-switches connected to different resistors.
                    The magnet can close 2 switches so that we have 16 positions.
                    The output of the voltage-divider is measured via MCP3008 (A2D).
 */

#ifndef HPCWINDVANE_H
#define HPCWINDVANE_H

#include "HPCMCP3008.h"
#include "HPCThreadedSensor.h"

/** \brief Sensor to measure the direction of wind.
 *  Gives the direction as value from 1 (North) to 16 (North/NorthWest) or 0 (invalid).
 *  Measures the output of the voltage-divider via mcp3008.
 */
class HPCWindVane : public HPCThreadedSensor
{
public:
    /** \brief Constructor
     *
     * \param id database id of the sensor.
     * \param channel The SPI channel of the connected mcp3008.
     * \param pin Inputpin of the mcp3008.
     * \param interval time (in seconds) to pause between querying the sensor.
     *
     */
    HPCWindVane( unsigned long id, unsigned int channel, unsigned int pin, unsigned int interval );
    /** \brief Destructor.
     */
    virtual ~HPCWindVane();

    /** \brief Get the SPI channel.
     *
     * \return The SPI channel (0/1).
     *
     */
    inline unsigned int Channel() const
      { return m_Channel; }

    /** \brief Get the type of the sensor.
     *
     * \return SensorType Type of the sensor (from enum SensorType).
     *
     */
    virtual inline SensorType Type() const
      { return SensorType::HPCWindVane; }

    /** \brief Get the direction in degrees.
     *
     * \return The direction in degrees.
     *
     */
    inline float Degrees() const
      { return m_Degrees; }

    /** \brief Get the direction as string.
     *
     * \return The direction as string ("N","NNE",NE",...).
     *
     */
    inline std::string dirString() const
      { return m_DirTxt; }

    /** \brief Get class with content as printable string.
     *
     * \return string
     *
     */
    virtual std::string asString() const;

protected:
    /** \brief Get a value from the sensor.
     */
    virtual void Measure();

    /** \brief Check if sensor has changed (called after interval pause).
     *
     * \return True if changed.
     */
    virtual bool hasChanged();

private:
    const unsigned int m_Channel; /**<  SPI channel */
    float m_Degrees; /**< The direction in degrees. */
    std::string m_DirTxt; /**< The direction as string */

    HPCMCP3008 *m_A2D; /**< The A/D converter-chip MCP3008 */
};

#endif // HPCWINDVANE_H
